#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author:Seran date:2018/8/21
from django.utils.deprecation import MiddlewareMixin
from django.http import JsonResponse
from utils.errors import genErrors
import pdb
import json
# import logging
# logger = logging.getLogger("userlog")
# from utils.CodeTools import userLogger
import ast
from user.urls import *
from storehouse.urls import *
from software.urls import *
from order.urls import *
from need.urls import *
from question.urls import *
from notice.urls import *
from log.urls import *
from system.urls import *
from announcement.urls import *

formDct = {
    loginAPI.urlPrefix + loginAPI.moduleUrl: loginAPI.form,
    registerAPI.urlPrefix + registerAPI.moduleUrl: registerAPI.form,
    logoutAPI.urlPrefix + logoutAPI.moduleUrl: logoutAPI.form,
    storehouseAddAPI.urlPrefix + storehouseAddAPI.moduleUrl: storehouseAddAPI.form,
    getStorehouseInfoAPI.urlPrefix + getStorehouseInfoAPI.moduleUrl: getStorehouseInfoAPI.form,
    uploadSoftAPI.urlPrefix + uploadSoftAPI.moduleUrl: uploadSoftAPI.form,
    uploadFilesAPI.urlPrefix + uploadFilesAPI.moduleUrl: uploadFilesAPI.form,
    softDetailAPI.urlPrefix + softDetailAPI.moduleUrl: softDetailAPI.form,
    softwareListAPI.urlPrefix + softwareListAPI.moduleUrl: softwareListAPI.form,
    softOperationAPI.urlPrefix + softOperationAPI.moduleUrl: softOperationAPI.form,
    softSearchAPI.urlPrefix + softSearchAPI.moduleUrl: softSearchAPI.form,
    orderListAPI.urlPrefix + orderListAPI.moduleUrl: orderListAPI.form,
    orderDetailAPI.urlPrefix + orderDetailAPI.moduleUrl: orderDetailAPI.form,
    orderDealAPI.urlPrefix + orderDealAPI.moduleUrl: orderDealAPI.form,
    orderSearchAPI.urlPrefix + orderSearchAPI.moduleUrl: orderSearchAPI.form,
    groupListAPI.urlPrefix + groupListAPI.moduleUrl: groupListAPI.form,
    groupSearchAPI.urlPrefix + groupSearchAPI.moduleUrl: groupSearchAPI.form,
    groupDetailAPI.urlPrefix + groupDetailAPI.moduleUrl: groupDetailAPI.form,
    groupOperateAPI.urlPrefix + groupOperateAPI.moduleUrl: groupOperateAPI.form,
    groupServiceOperateAPI.urlPrefix + groupServiceOperateAPI.moduleUrl: groupServiceOperateAPI.form,
    getSoftNoticeAPI.urlPrefix + getSoftNoticeAPI.moduleUrl: getSoftNoticeAPI.form,
    getRunLogAPI.urlPrefix + getRunLogAPI.moduleUrl: getRunLogAPI.form,
    groupDownLoadAPI.urlPrefix + groupDownLoadAPI.moduleUrl: groupDownLoadAPI.form,
    groupOrderListAPI.urlPrefix + groupOrderListAPI.moduleUrl: groupOrderListAPI.form,

    changeStorehouseInfoAPI.urlPrefix + changeStorehouseInfoAPI.moduleUrl: changeStorehouseInfoAPI.form,
    # getIPListAPI.urlPrefix + getIPListAPI.moduleUrl: getIPListAPI.form,
    IPChangeAPI.urlPrefix + IPChangeAPI.moduleUrl: IPChangeAPI.form,
    getSubStoreListAPI.urlPrefix + getSubStoreListAPI.moduleUrl: getSubStoreListAPI.form,
    subStorehouseMsgAPI.urlPrefix + subStorehouseMsgAPI.moduleUrl: subStorehouseMsgAPI.form,
    getWarZoneListAPI.urlPrefix + getWarZoneListAPI.moduleUrl: getWarZoneListAPI.form,
    getCompanyListAPI.urlPrefix + getCompanyListAPI.moduleUrl: getCompanyListAPI.form,
    storehouseOperationAPI.urlPrefix + storehouseOperationAPI.moduleUrl: storehouseOperationAPI.form,
    storeScreenAPI.urlPrefix + storeScreenAPI.moduleUrl: storeScreenAPI.form,
    addNeedAPI.urlPrefix + addNeedAPI.moduleUrl: addNeedAPI.form,
    getNeedListAPI.urlPrefix + getNeedListAPI.moduleUrl: getNeedListAPI.form,
    addQuestionAPI.urlPrefix + addQuestionAPI.moduleUrl: addQuestionAPI.form,
    getQuestionListAPI.urlPrefix + getQuestionListAPI.moduleUrl: getQuestionListAPI.form,
    questionOptionAPI.urlPrefix + questionOptionAPI.moduleUrl: questionOptionAPI.form,
    needOptionAPI.urlPrefix + needOptionAPI.moduleUrl: needOptionAPI.form,
    dataOperationAPI.urlPrefix + dataOperationAPI.moduleUrl: dataOperationAPI.form,
    dataCheckAPI.urlPrefix + dataCheckAPI.moduleUrl: dataCheckAPI.form,
    getDataListAPI.urlPrefix + getDataListAPI.moduleUrl: getDataListAPI.form,
    dataScreenAPI.urlPrefix + dataScreenAPI.moduleUrl: dataScreenAPI.form,
    needScreenAPI.urlPrefix + needScreenAPI.moduleUrl: needScreenAPI.form,
    questionScreenAPI.urlPrefix + questionScreenAPI.moduleUrl: questionScreenAPI.form,
    getStorehouseListAPI.urlPrefix + getStorehouseListAPI.moduleUrl: getStorehouseListAPI.form,
    getSelectAPI.urlPrefix + getSelectAPI.moduleUrl: getSelectAPI.form,
    getValidImgAPI.urlPrefix + getValidImgAPI.moduleUrl: getValidImgAPI.form,
    getTypeListAPI.urlPrefix + getTypeListAPI.moduleUrl: getTypeListAPI.form,
    addNoticeAPI.urlPrefix + addNoticeAPI.moduleUrl: addNoticeAPI.form,
    getNoticeAPI.urlPrefix + getNoticeAPI.moduleUrl: getNoticeAPI.form,
    noticeContentAPI.urlPrefix + noticeContentAPI.moduleUrl: noticeContentAPI.form,
    getIPListAPI.urlPrefix + getIPListAPI.moduleUrl: getIPListAPI.form,
    IPLogScreenAPI.urlPrefix + IPLogScreenAPI.moduleUrl: IPLogScreenAPI.form,
    IPOperateAPI.urlPrefix + IPOperateAPI.moduleUrl: IPOperateAPI.form,
    optionNoticeAPI.urlPrefix + optionNoticeAPI.moduleUrl: optionNoticeAPI.form,
    systemStatusGetAPI.urlPrefix + systemStatusGetAPI.moduleUrl: systemStatusGetAPI.form,
    getLogAPI.urlPrefix + getLogAPI.moduleUrl: getLogAPI.form,
    logScreenAPI.urlPrefix + logScreenAPI.moduleUrl: logScreenAPI.form,
    logCheckAPI.urlPrefix + logCheckAPI.moduleUrl: logCheckAPI.form,
    systemScreenAPI.urlPrefix + systemScreenAPI.moduleUrl: systemScreenAPI.form,
    dataUpdateAPI.urlPrefix + dataUpdateAPI.moduleUrl: dataUpdateAPI.form,
    questionAnnounceAPI.urlPrefix + questionAnnounceAPI.moduleUrl: questionAnnounceAPI.form,
    needAnnounceAPI.urlPrefix + needAnnounceAPI.moduleUrl: needAnnounceAPI.form,
    isOnlineAPI.urlPrefix + isOnlineAPI.moduleUrl: isOnlineAPI.form,
    getTypeAPI.urlPrefix + getTypeAPI.moduleUrl: getTypeAPI.form,
    softwareTypeOptionAPI.urlPrefix + softwareTypeOptionAPI.moduleUrl: softwareTypeOptionAPI.form,
    warzoneGetAPI.urlPrefix + warzoneGetAPI.moduleUrl: warzoneGetAPI.form,
    zoneOptionAPI.urlPrefix + zoneOptionAPI.moduleUrl: zoneOptionAPI.form,
    getReadAPI.urlPrefix + getReadAPI.moduleUrl: getReadAPI.form,
    companyGetAPI.urlPrefix + companyGetAPI.moduleUrl: companyGetAPI.form,
    companyOptAPI.urlPrefix + companyOptAPI.moduleUrl: companyOptAPI.form,
    detailGetAPI.urlPrefix + detailGetAPI.moduleUrl: detailGetAPI.form,
    qaGetAPI.urlPrefix + qaGetAPI.moduleUrl: qaGetAPI.form,
    indexAPI.urlPrefix + detailGetAPI.moduleUrl: detailGetAPI.form,
    checkChunkAPI.urlPrefix + detailGetAPI.moduleUrl: detailGetAPI.form,
    mergeChunksAPI.urlPrefix + detailGetAPI.moduleUrl: detailGetAPI.form,
    uploadNewAPI.urlPrefix + detailGetAPI.moduleUrl: detailGetAPI.form,
    deleteAPI.urlPrefix + deleteAPI.moduleUrl: deleteAPI.form,
    getHashAPI.urlPrefix + getHashAPI.moduleUrl: getHashAPI.form,
    uploadChunkAPI.urlPrefix + uploadChunkAPI.moduleUrl: uploadChunkAPI.form,
    updateTokenAPI.urlPrefix + updateTokenAPI.moduleUrl: updateTokenAPI.form,
    getDuanInfoAPI.urlPrefix + getDuanInfoAPI.moduleUrl: getDuanInfoAPI.form,
    getDuanInfoServerAPI.urlPrefix + getDuanInfoServerAPI.moduleUrl: getDuanInfoServerAPI.form,
    pushDuanToOtherAPI.urlPrefix + pushDuanToOtherAPI.moduleUrl: pushDuanToOtherAPI.form,
    containerListAPI.urlPrefix + containerListAPI.moduleUrl: containerListAPI.form,
    getPushListAPI.urlPrefix + getPushListAPI.moduleUrl: getPushListAPI.form,
    getPushStateAPI.urlPrefix + getPushStateAPI.moduleUrl: getPushStateAPI.form,
    supervisorUserListAPI.urlPrefix + supervisorUserListAPI.moduleUrl: supervisorUserListAPI.form,
    supervisorEditAPI.urlPrefix + supervisorEditAPI.moduleUrl: supervisorEditAPI.form,

    searchUserAPI.urlPrefix + searchUserAPI.moduleUrl: searchUserAPI.form,
}

methodDct = {
    loginAPI.urlPrefix + loginAPI.moduleUrl: loginAPI.method,
    registerAPI.urlPrefix + registerAPI.moduleUrl: registerAPI.method,
    logoutAPI.urlPrefix + logoutAPI.moduleUrl: logoutAPI.method,
    storehouseAddAPI.urlPrefix + storehouseAddAPI.moduleUrl: storehouseAddAPI.method,
    getStorehouseInfoAPI.urlPrefix + getStorehouseInfoAPI.moduleUrl: getStorehouseInfoAPI.method,
    uploadSoftAPI.urlPrefix + uploadSoftAPI.moduleUrl: uploadSoftAPI.method,
    uploadFilesAPI.urlPrefix + uploadFilesAPI.moduleUrl: uploadFilesAPI.method,
    softDetailAPI.urlPrefix + softDetailAPI.moduleUrl: softDetailAPI.method,
    softwareListAPI.urlPrefix + softwareListAPI.moduleUrl: softwareListAPI.method,
    softOperationAPI.urlPrefix + softOperationAPI.moduleUrl: softOperationAPI.method,
    softSearchAPI.urlPrefix + softSearchAPI.moduleUrl: softSearchAPI.method,
    orderListAPI.urlPrefix + orderListAPI.moduleUrl: orderListAPI.method,
    orderDetailAPI.urlPrefix + orderDetailAPI.moduleUrl: orderDetailAPI.method,
    orderDealAPI.urlPrefix + orderDealAPI.moduleUrl: orderDealAPI.method,
    orderSearchAPI.urlPrefix + orderSearchAPI.moduleUrl: orderSearchAPI.method,
    groupListAPI.urlPrefix + groupListAPI.moduleUrl: groupListAPI.method,
    groupSearchAPI.urlPrefix + groupSearchAPI.moduleUrl: groupSearchAPI.method,
    groupDetailAPI.urlPrefix + groupDetailAPI.moduleUrl: groupDetailAPI.method,
    groupOperateAPI.urlPrefix + groupOperateAPI.moduleUrl: groupOperateAPI.method,
    groupServiceOperateAPI.urlPrefix + groupServiceOperateAPI.moduleUrl: groupServiceOperateAPI.method,
    getSoftNoticeAPI.urlPrefix + getSoftNoticeAPI.moduleUrl: getSoftNoticeAPI.method,
    getRunLogAPI.urlPrefix + getRunLogAPI.moduleUrl: getRunLogAPI.method,
    groupDownLoadAPI.urlPrefix + groupDownLoadAPI.moduleUrl: groupDownLoadAPI.method,
    groupOrderListAPI.urlPrefix + groupOrderListAPI.moduleUrl: groupOrderListAPI.method,

    changeStorehouseInfoAPI.urlPrefix + changeStorehouseInfoAPI.moduleUrl: changeStorehouseInfoAPI.method,
    # getIPListAPI.urlPrefix + getIPListAPI.moduleUrl: getIPListAPI.method,
    IPChangeAPI.urlPrefix + IPChangeAPI.moduleUrl: IPChangeAPI.method,
    getSubStoreListAPI.urlPrefix + getSubStoreListAPI.moduleUrl: getSubStoreListAPI.method,
    subStorehouseMsgAPI.urlPrefix + subStorehouseMsgAPI.moduleUrl: subStorehouseMsgAPI.method,
    getWarZoneListAPI.urlPrefix + getWarZoneListAPI.moduleUrl: getWarZoneListAPI.method,
    getCompanyListAPI.urlPrefix + getCompanyListAPI.moduleUrl: getCompanyListAPI.method,
    storehouseOperationAPI.urlPrefix + storehouseOperationAPI.moduleUrl: storehouseOperationAPI.method,
    storeScreenAPI.urlPrefix + storeScreenAPI.moduleUrl: storeScreenAPI.method,
    addNeedAPI.urlPrefix + addNeedAPI.moduleUrl: addNeedAPI.method,
    getNeedListAPI.urlPrefix + getNeedListAPI.moduleUrl: getNeedListAPI.method,
    addQuestionAPI.urlPrefix + addQuestionAPI.moduleUrl: addQuestionAPI.method,
    getQuestionListAPI.urlPrefix + getQuestionListAPI.moduleUrl: getQuestionListAPI.method,
    questionOptionAPI.urlPrefix + questionOptionAPI.moduleUrl: questionOptionAPI.method,
    needOptionAPI.urlPrefix + needOptionAPI.moduleUrl: needOptionAPI.method,
    dataOperationAPI.urlPrefix + dataOperationAPI.moduleUrl: dataOperationAPI.method,
    dataCheckAPI.urlPrefix + dataCheckAPI.moduleUrl: dataCheckAPI.method,
    getDataListAPI.urlPrefix + getDataListAPI.moduleUrl: getDataListAPI.method,
    dataScreenAPI.urlPrefix + dataScreenAPI.moduleUrl: dataScreenAPI.method,
    needScreenAPI.urlPrefix + needScreenAPI.moduleUrl: needScreenAPI.method,
    questionScreenAPI.urlPrefix + questionScreenAPI.moduleUrl: questionScreenAPI.method,
    getStorehouseListAPI.urlPrefix + getStorehouseListAPI.moduleUrl: getStorehouseListAPI.method,
    getSelectAPI.urlPrefix + getSelectAPI.moduleUrl: getSelectAPI.method,
    getValidImgAPI.urlPrefix + getValidImgAPI.moduleUrl: getValidImgAPI.method,
    getTypeListAPI.urlPrefix + getTypeListAPI.moduleUrl: getTypeListAPI.method,
    getNoticeAPI.urlPrefix + getNoticeAPI.moduleUrl: getNoticeAPI.method,
    addNoticeAPI.urlPrefix + addNoticeAPI.moduleUrl: addNoticeAPI.method,
    noticeContentAPI.urlPrefix + noticeContentAPI.moduleUrl: noticeContentAPI.method,
    getIPListAPI.urlPrefix + getIPListAPI.moduleUrl: getIPListAPI.method,
    IPLogScreenAPI.urlPrefix + IPLogScreenAPI.moduleUrl: IPLogScreenAPI.method,
    IPOperateAPI.urlPrefix + IPOperateAPI.moduleUrl: IPOperateAPI.method,
    optionNoticeAPI.urlPrefix + optionNoticeAPI.moduleUrl: optionNoticeAPI.method,
    systemStatusGetAPI.urlPrefix + systemStatusGetAPI.moduleUrl: systemStatusGetAPI.method,
    getLogAPI.urlPrefix + getLogAPI.moduleUrl: getLogAPI.method,
    logScreenAPI.urlPrefix + logScreenAPI.moduleUrl: logScreenAPI.method,
    logCheckAPI.urlPrefix + logCheckAPI.moduleUrl: logCheckAPI.method,
    systemScreenAPI.urlPrefix + systemScreenAPI.moduleUrl: systemScreenAPI.method,
    dataUpdateAPI.urlPrefix + dataUpdateAPI.moduleUrl: dataUpdateAPI.method,
    questionAnnounceAPI.urlPrefix + questionAnnounceAPI.moduleUrl: questionAnnounceAPI.method,
    needAnnounceAPI.urlPrefix + needAnnounceAPI.moduleUrl: needAnnounceAPI.method,
    isOnlineAPI.urlPrefix + isOnlineAPI.moduleUrl: isOnlineAPI.method,
    getTypeAPI.urlPrefix + getTypeAPI.moduleUrl: getTypeAPI.method,
    softwareTypeOptionAPI.urlPrefix + softwareTypeOptionAPI.moduleUrl: softwareTypeOptionAPI.method,
    warzoneGetAPI.urlPrefix + warzoneGetAPI.moduleUrl: warzoneGetAPI.method,
    zoneOptionAPI.urlPrefix + zoneOptionAPI.moduleUrl: zoneOptionAPI.method,
    getReadAPI.urlPrefix + getReadAPI.moduleUrl: getReadAPI.method,
    companyGetAPI.urlPrefix + companyGetAPI.moduleUrl: companyGetAPI.method,
    companyOptAPI.urlPrefix + companyOptAPI.moduleUrl: companyOptAPI.method,
    detailGetAPI.urlPrefix + detailGetAPI.moduleUrl: detailGetAPI.method,
    qaGetAPI.urlPrefix + qaGetAPI.moduleUrl: qaGetAPI.method,
    indexAPI.urlPrefix + detailGetAPI.moduleUrl: detailGetAPI.method,
    checkChunkAPI.urlPrefix + detailGetAPI.moduleUrl: detailGetAPI.method,
    mergeChunksAPI.urlPrefix + detailGetAPI.moduleUrl: detailGetAPI.method,
    uploadNewAPI.urlPrefix + detailGetAPI.moduleUrl: detailGetAPI.method,
    deleteAPI.urlPrefix + deleteAPI.moduleUrl: deleteAPI.method,
    getHashAPI.urlPrefix + getHashAPI.moduleUrl: getHashAPI.method,
    uploadChunkAPI.urlPrefix + uploadChunkAPI.moduleUrl: uploadChunkAPI.method,
    updateTokenAPI.urlPrefix + updateTokenAPI.moduleUrl: updateTokenAPI.method,
    getDuanInfoAPI.urlPrefix + getDuanInfoAPI.moduleUrl: getDuanInfoAPI.method,
    getDuanInfoServerAPI.urlPrefix + getDuanInfoServerAPI.moduleUrl: getDuanInfoServerAPI.method,
    pushDuanToOtherAPI.urlPrefix + pushDuanToOtherAPI.moduleUrl: pushDuanToOtherAPI.method,
    containerListAPI.urlPrefix + containerListAPI.moduleUrl: containerListAPI.method,
    getPushListAPI.urlPrefix + getPushListAPI.moduleUrl: getPushListAPI.method,
    getPushStateAPI.urlPrefix + getPushStateAPI.moduleUrl: getPushStateAPI.method,
    supervisorUserListAPI.urlPrefix + supervisorUserListAPI.moduleUrl: supervisorUserListAPI.method,
    supervisorEditAPI.urlPrefix + supervisorEditAPI.moduleUrl: supervisorEditAPI.method,
    searchUserAPI.urlPrefix + searchUserAPI.moduleUrl: searchUserAPI.method,
}


class MyCorsMiddle(MiddlewareMixin):
    def process_response(self, request, response):
        print('11111111')
        pdb.set_trace()
        if request.method == 'OPTIONS':
            response['Access-Control-Allow-Headers'] = 'Content-Type'
            response['Access-Control-Allow-Methods'] = '*'
        return response


class FormCheckMiddleware(MiddlewareMixin):

    def process_request(self, request):
        # 路径是否正确
        if (request.path in methodDct):
            # 检测方法
            if request.method != methodDct[request.path]:
                data = genErrors("WrongMethod")
                # logger.info(json.dumps(data))
                return JsonResponse(data)
            try:
                formInst = formDct[request.path](json.loads(request.body.decode('utf-8')))
            except:
                try:
                    if (request.method == "POST"):
                        formInst = formDct[request.path](request.POST)
                    else:
                        formInst = formDct[request.path](request.GET)
                except:
                    data = genErrors("InvalidForm")
                    return JsonResponse(data)
            if not formInst.is_valid():
                # pdb.set_trace()
                data1 = {
                    'code': 1,
                    # 'requestParam': ,
                }
                all_error = formInst.errors
                # all = formInst.errors.get('__all__')
                # print(all)
                if all_error:
                    for a in all_error:
                        # print(all_error[a][0])
                        # if all_error[a][0] in genErrors:
                        #     data1.append(genErrors(all_error[a][0]))
                        # else:
                        data1[a] = (all_error[a][0])
                return JsonResponse(data1)
            else:
                request.checkForm = formInst

    def process_response(self, request, response):
        # print(request.method)
        # pdb.set_trace()
        if request.method not in ['POST', 'GET']:
            response['Access-Control-Allow-Headers'] = 'Content-Type'
            response['Access-Control-Allow-Methods'] = '*'

        return response
